﻿using UnityEngine;
using System.Collections;

public class LoadAssetBoundle : MonoBehaviour {


    void OnGUI() {
        if (GUILayout.Button("WWW"))
        {
            StartCoroutine(UseWWW());
        }
        
        if (GUILayout.Button("LoadFromCacheOrDownload"))
        {
            StartCoroutine(UserLoadFromCacheOrDownload());
        }



        /*
        if (GUILayout.Button("LoadFromFile"))
        {
            LoadFromFile();
        }
        if (GUILayout.Button("LoadFromFileAsync"))
        {
            StartCoroutine("LoadFromFileAsync");
        }*/

        if (GUILayout.Button("Set Cube green Material"))
        {

            if (greendAssetBundle != null)
            {
                Object o = greendAssetBundle.LoadAsset("green");
                Material mat = o as Material;
                greencube.GetComponent<MeshRenderer>().material = mat;
            }
        }

        if (GUILayout.Button("Load UI"))
        {

            if (greendAssetBundle != null)
            {
                Object o = greendAssetBundle.LoadAsset("Canvas_A");
                Instantiate(o as GameObject);
            }
        }



    }
    public AssetBundle greendAssetBundle;
    public GameObject greencube;
    Object o;
    IEnumerator UseWWW()
    {
        string path = "file://" + Application.streamingAssetsPath + "/Windows/cube_green";
        WWW www = new WWW(path);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            greendAssetBundle = www.assetBundle;
             o = greendAssetBundle.LoadAsset("Cube_Green");
            greencube = Instantiate(o as GameObject);
            Resources.UnloadAsset(o);
            Resources.UnloadUnusedAssets();

        }
        yield break;
    }
    public AssetBundle uiAssetBoundle;
    
    IEnumerator UserLoadFromCacheOrDownload()
    {
        string fileName = "file://" + Application.streamingAssetsPath + "/Windows/ui.pc";
        WWW www = WWW.LoadFromCacheOrDownload(fileName,0);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
        {
            uiAssetBoundle = www.assetBundle;
        }
        yield break;
    }
    void LoadFromFile()
    {
        string fileName = Application.streamingAssetsPath + "/Windows/cube_red";
        AssetBundle assetBundle = AssetBundle.LoadFromFile(fileName);
        if (assetBundle != null)
        {
            Object o = assetBundle.LoadAsset("cube_red");
            GameObject go = o as GameObject;
            GameObject go2 = Instantiate(go);
        }
    }

    IEnumerator LoadFromFileAsync()
    {
        string fileName = Application.streamingAssetsPath + "/Windows/cube_green";
        AssetBundleCreateRequest assetRequest = AssetBundle.LoadFromFileAsync(fileName);
        yield return assetRequest;
        AssetBundle assetBundle = assetRequest.assetBundle;
        if (assetBundle != null)
        {
            Object o = assetBundle.LoadAsset("cube_green");
            GameObject go = o as GameObject;
            GameObject go2 = Instantiate(go);
        }
    }
}
